package main

import (
	"bufio"
	"fmt"
	"io"
	"net"
)

// server
func main() {
	listen, err := net.Listen("tcp", "localhost:8888")
	if err != nil {
		fmt.Println("server err, err is ", err)
	}
	defer listen.Close()
	for {
		// wait client
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println(err)
			continue
		}
		go func(conn net.Conn) {
			defer conn.Close()
			for {
				buf := make([]byte, 1024)
				if n, err := bufio.NewReader(conn).Read(buf); err != io.EOF {
					fmt.Printf("ip is %v, content is %v\n", conn.RemoteAddr(), string(buf[:n]))
				} else {
					// fmt.Println(err)
					return
				}
			}
		}(conn)

	}
}
